<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/3/7 0007
 * Time: 20:59
 */

namespace app\common\widget;
use app\common\library\CatchException;
use app\common\library\CommonFun;
use Exception;

/**
 * Class WidgetToolTip
 * @package app\common\widget
 * @method $this setPlacementTop()
 * @method $this setPlacementLeft()
 * @method $this setPlacementRight()
 * @method $this setPlacementBottom()
 * @method WidgetIcons addIcon() 添加一个ICON图标
 *
 */
class WidgetToolTip extends WidgetCommon
{

    /**
     * ICON
     * @var WidgetIcons
     */
    private $icon ;

    /**
     * 设置Action属性
     * @var array
     */
    private $action = [];

    /**
     * 是否是ajax请求
     * @var bool
     */
    //private $is_ajax = false;

    /**
     * 构造方法, 渲染挂件
     * @return mixed
     * @throws \think\Exception
     * @throws Exception
     */
    function render()
    {
        $this->beforeRender();

        // 分配图标
        self::$view->assign('icon', $this->icon->render());
        // 分配Ajax标示
        //self::$view->assign( 'is_ajax', $this->is_ajax );

        // 渲染模版
        return $this->fetch();
    }

    /**
     * Before Render
     * @return $this
     * @throws Exception
     */
    private function beforeRender() {
        // 检测
        if ( is_null($this->icon) ) throw new Exception("挂件ToolTip没有ICON", CatchException::WidgetErrorException);
        if ( !$this->getDomAttr('title') ) throw new Exception("挂件ToolTip没有设置Title", CatchException::WidgetErrorException);
        // 判断是否有ID
        if ( !$this->getId() ) $this->setId( CommonFun::create_unique_id() );
        // 生成action属性
        $this->createAction();
        return $this;
    }


    /**
     * 初始化
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();
        // 设置默认class
        $this->setClass(['btn','btn-box-tool']);
        // 设置href
        $this->setDomAttr('href', 'javascript:void(0);');
        // 设置data-toggle
        $this->setDomAttr('data-toggle', 'tooltip');
        // 设置data-placement
        $this->setPlacementTop();
    }

    /**
     * 设置ToolTip操作地址
     * @param string $controller   控制器名称
     * @param string $action       操作方法名称
     * @param array $param         请求参数
     * @return $this
     */
    public function setAction( $controller, $action, array $param=array() ) {
        $this->action = compact('controller', 'action', 'param');
        return $this;
    }

    /**
     * 设置ToolTip操作地址参数
     * @param array $param
     * @return $this
     */
    public function setActionParam( array $param ) {
        $this->action['param'] = isset( $this->action['param'] ) ? array_merge( $this->action['param'], $param ) : $param;
        return $this;
    }


    /**
     * 生成action属性
     * @return $this
     */
    private function createAction() {
        if ( $this->action ) {
            /**
             * @var string $controller
             * @var string $action
             * @var array $param
             */
            extract($this->action);
            $path = "{$controller}/{$action}";
            $this->setDomAttr('action', url( $path, $param ));
        }
        return $this;
    }

    /**
     * 设置请求ajax
     * @param $jsCallBackFuncton
     * @return $this
     */
//    public function setAjax( $jsCallBackFuncton ) {
//        $this->is_ajax = true;
//        $this->setDomAttr('jsCallBack', $jsCallBackFuncton);
//        return $this;
//    }

    /**
     * 设置Title
     * @param $title
     * @return $this
     */
    public function setTitle( $title ) {
        $this->setDomAttr('title', $title);
        return $this;
    }

    /**
     * 设置一个ICON
     * @param WidgetIcons $iconObj
     * @return $this
     */
    private function setIcon ( WidgetIcons &$iconObj ) {
        $this->icon = $iconObj;
        return $this;
    }

    /**
     * 设置Title展示方向
     * @param $place
     * @return $this
     */
    private function setPlacement( $place ) {
        $this->setDomAttr('data-placement', $place);
        return $this;
    }

    /**
     * 获取ToolTip操作地址参数
     * @return array
     */
    public function getAction(): array
    {
        return $this->action;
    }

    /**
     * 未定义方法回调
     * @param $name
     * @param $arguments
     * @return WidgetToolTip|WidgetIcons
     * @throws Exception
     */
    public function __call($name, $arguments)
    {
        // 匹配位置
        if ( strpos( $name, 'setPlacement' ) !==false ) {
            $style = str_replace( "setPlacement", "", $name );
            $place = strtolower($style);
            return $this->setPlacement($place);
        }
        // 添加图标
        if ( $name == "addIcon" ) {
            $iconObj = new WidgetIcons();
            $this->setIcon($iconObj);
            return $iconObj;
        }

        return $this;
    }


}